package cn.dansj.entity;

import cn.dansj.bean.TableConstant;
import cn.dansj.enums.GlobalEnv;
import cn.dansj.common.utils.json.DictMap;
import cn.dansj.common.utils.transfer.DateTime;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import jakarta.persistence.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.Serial;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Map;

@Entity
@Table(name = TableConstant.requestLog)
public class RequestLog implements Serializable {
    @Serial
    private static final long serialVersionUID = GlobalEnv.SerialVersionUID;
    @Id
    private String sequence;
    private String controller;
    private String url;
    private String params;
    private String loginUser;
    private int status;
    private String method;
    private String fullException;
    private String sketch;
    private String token;
    @Column(name = "start_time")
    private Timestamp responseStartTime;
    @Column(name = "end_time")
    private Timestamp responseEndTime;
    private long cost;
    private String ipInfo;
    private String referer;
    private String responseCode;
    private String userAgent;
    private int apiType;
    private Timestamp crtTime;

    public String getSequence() {
        return sequence;
    }

    public void setSequence(String sequence) {
        this.sequence = sequence;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getParams() {
        return params;
    }

    public void setParams(String params) {
        this.params = params;
    }

    /**
     * 去除长度超过限制的key，防止数据库保存失败
     */
    public void setParams(DictMap params) {
        params.removeIf(e -> e.getKey().length() > 50);
        Map<String, Object> objectMap = params.getIf(e -> e.getValue() instanceof MultipartFile);
        objectMap.forEach((k, v) -> {
            params.put(k, ((MultipartFile) v).getOriginalFilename());
        });
        this.params = params.toJSONString();
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public Timestamp getResponseStartTime() {
        return responseStartTime;
    }

    public void setResponseStartTime(Timestamp responseStartTime) {
        this.responseStartTime = responseStartTime;
    }

    public Timestamp getResponseEndTime() {
        return responseEndTime;
    }

    public void setResponseEndTime(Timestamp responseEndTime) {
        this.responseEndTime = responseEndTime;
    }

    public Timestamp getCrtTime() {
        return crtTime;
    }

    public void setCrtTime(Timestamp crtTime) {
        this.crtTime = crtTime;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getFullException() {
        return fullException;
    }

    public void setFullException(String fullException) {
        this.fullException = fullException;
    }

    public String getSketch() {
        return sketch;
    }

    public void setSketch(String sketch) {
        this.sketch = sketch;
    }

    @Transient
    public String getStartTimestamp() {
        return DateTime.getInstance(responseStartTime).stringTimestamp();
    }

    @Transient
    public String getEndTimestamp() {
        return DateTime.getInstance(responseEndTime).stringTimestamp();
    }

    public long getCost() {
        return cost;
    }

    public void setCost(long cost) {
        this.cost = cost;
    }

    public void setController(String controller) {
        this.controller = controller;
    }

    public String getController() {
        return controller;
    }

    public String getLoginUser() {
        return loginUser;
    }

    public void setLoginUser(String loginUser) {
        this.loginUser = loginUser;
    }

    public String getUserAgent() {
        return userAgent;
    }

    public void setUserAgent(String userAgent) {
        this.userAgent = userAgent;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public String getResponseCode() {
        return responseCode;
    }

    public void setResponseCode(String responseCode) {
        this.responseCode = responseCode;
    }

    public JSONObject getIpInfo() {
        return JSON.parseObject(ipInfo);
    }

    public void setIpInfo(String ipInfo) {
        this.ipInfo = ipInfo;
    }

    public String getReferer() {
        return referer;
    }

    public void setReferer(String referer) {
        this.referer = referer;
    }

    public int getApiType() {
        return apiType;
    }

    public void setApiType(int apiType) {
        this.apiType = apiType;
    }
}
